package com.app.miwawa.api.system.service.bo;

import javax.annotation.Resource;

import org.apache.commons.lang3.ClassUtils;
import org.framework.common.api.entity.CommonEntityAware;
import org.framework.common.api.service.bo.CommonBoAdapter;
import org.framework.common.api.service.dao.CommonDaoAware;
import org.framework.common.api.support.constant.SystemConstants;
import org.framework.common.ext.util.ClassUtil;
import org.framework.common.ext.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.app.miwawa.api.system.entity.Easemob;
import com.app.miwawa.api.system.entity.center.Depts;
import com.app.miwawa.api.system.entity.center.Relation;
import com.app.miwawa.api.system.entity.center.Users;
import com.app.miwawa.api.system.service.dao.UsersDao;
import com.easemob.server.example.utils.EasemobUtils;

/**
 * 业务接口
 * <p>描述: 雇员－业务接口</p>
 * <p>Company: Professional</p>
 * @author chanlong(陈龙)
 * @date 2016年1月23日 上午9:37:10
 * @version 1.0.2016
 */
@Service("usersBo")
public class UsersBo extends CommonBoAdapter<Relation, String> {
	
	@Autowired
	private UsersDao usersDao;
	
	@Resource(name = "defaultDao")
	private CommonDaoAware<CommonEntityAware<String>, String> dao;

	/**
	 * @see org.framework.common.api.service.bo.CommonBoAdapter#insert(org.framework.common.api.entity.CommonEntityAware)
	 * @author chanlong(陈龙)
	 * @date 2016年1月23日 上午9:44:41
	 */
	@Override
	public Relation insert(Relation relation) throws Exception {
		Depts depts = relation.getDepts();
		Users users = relation.getUsers();
		
		users.setAccount(StringUtil.toPinyin(users.getName()));
		users.setPassword(StringUtil.DEFAULT_PASSWORD);
		
		// 保存用户及关系信息
		users = usersDao.save(users);
		dao.insert(createRelation(depts, users));
		
		String uid = users.getId();
		String eid = "U"+uid;
		String type = users.getPost();
		String shops = depts.getShopsid();
		String mobile = users.getMobile();
		String nickname = users.getName();
		String password = users.getPassword();
		
		// 环信注册成功后，保存环信注册记录
		if(EasemobUtils.createIMUser(eid, password, nickname)){
			dao.insert(new Easemob(uid, eid, type, mobile, shops));
		}
		
		relation.setUsers(users);
		return relation;
	}
	
	/**
	 * @see org.framework.common.api.service.bo.CommonBoAdapter#update(org.framework.common.api.entity.CommonEntityAware)
	 * @author chanlong(陈龙)
	 * @date 2016年3月29日 上午11:08:51
	 */
	@Override
	public Relation update(Relation relation) throws Exception {
		Users users = relation.getUsers();
		
		users.setAccount(StringUtil.toPinyin(users.getName()));
		
		Users entity = usersDao.findOne(users.getId());
		ClassUtil.copyProperties(users, entity);
		users = usersDao.saveAndFlush(entity);
		
		relation.setUsers(users);
		
		return relation;
	}
	
	/**
	 * 修改密码.
	 * @param relation
	 * @throws Exception
	 * @author chanlong(陈龙)
	 * @date 2016年4月12日 下午12:13:51
	 */
	public void password(Relation relation) throws Exception {
		Users users = relation.getUsers();
		usersDao.password(users.getPassword(), users.getId());
		
		// 修改环信账号
		EasemobUtils.resetPassword("U"+users.getId(), users.getPassword(), users.getName());
	}
	
	/*
	 * 创建部门和员工关系
	 */
	private CommonEntityAware<String> createRelation(final Depts depts, final Users users){
		Relation relation = new Relation(ClassUtils.getSimpleName(Users.class), users.getId(), ClassUtils.getSimpleName(Depts.class), depts.getId());
		relation.setStatus(SystemConstants.DICT_STATUS_DEFAULT);
		relation.setInvalid(SystemConstants.DICT_DELETE_INVALID);
		return relation;
	}
}
